<extend name="layout/main" />

<block name="HEADER"></block>

<block name="CONTENT">
	
	<control:grid id="grid"/>
	
	<script id="tplScript" type="text/html">
		<form class="layui-form J-form">
			<table class="xw-table">
				<tr><th width="90"></th><th></th></tr>
				<tr>
					<td align="center">当前等级：</td>
					<td> {{level}} </td>
				</tr>
				<tr>
					<td align="center" class="f-orage">变更等级：</td>
					<td>
					    <select name="level" lay-verify="required" datatype="*">
					     	{{each list as val}}
					     		<option value="{{val.value}}">{{val.label}}</option>
					     	{{/each}}
					    </select>
					</td>
				</tr>
			</table>
		</form>
	</script>	
	
</block>

<block name="FOOTER">
	<script type="text/javascript">
		var page = (function(ns){
     
		    //页面初始化
		    ns.init=function(){

		    }
		    
		    //新增
		    ns.add=function(){
		    	var filter = {
		            url:$.getModule('news'),
		            oid:"",
		            mode:1
		        }
		        $.openIframe(filter, "新增会员卡",['600px', '500px']);
		    }
		    
		    //编辑
		    ns.edit=function(){
		    	var selectedRow = grid.getSelectedRows()[0];
		        var filter = {
		            url:$.getModule('edit'),
		            oid: selectedRow.id ,
		            mode:3
		        }
		        $.openIframe(filter, "会员详情",['800px', '92%']);
		    }  
		    
		    
		    //删除
		    ns.del =function(){
		    	if( !grid.isSelected() ){
		    		return false;
		    	}
		    	$.confirm("确定删除？", function(){
		    		$.ajaxs('/admin/v1/Spread/del',{
			    		ids:grid.getSelectedColumnValue('id')
			    	},function(res){
			    		$.toast(1,"已删除！");
			    		grid.load();
			    	});
		    	});
		    }
		    
		    
		    //升级
		    ns.upgrade =function(id,levelName){
		    	if( !grid.isSelected() ){
		    		return false;
		    	}
		    	function successFn(){
		    		$.ajaxs("/admin/v1/user_level/xmlGetLevel").then(function(res){
			            $("#level").html( template("tplScript",{ list:res.data,level:levelName }));
			           	layui.form.render(null,null,'.J-form');
			        });
		    	}
		    	function yesFn(index){
		    		$(".J-form").validformIn(function(data){ 
		    			data.uid = id;
		                $.ajaxs("/admin/v1/user_vip/changeLevel",data,function(res){
		                    $.toast(1,res.msg);
		                    grid.load();
		                    layer.close(index);
		                });
			        });
		    	}
		    	$.openModalDialog( '<div id="level"></div>','变更等级',successFn,yesFn,['300px', '480px']);
		    }
		    
		    //切换店铺
		    ns.changeShop =function(uid,shop_id){
		    	var shopGrid;
		    	function successFn(){
             		shopGrid = layui.grid.render("shopGrid"); 
             		shopGrid.load([{"field":"id","opera":"<>","value":shop_id}]);    
		    	}
		    	function yesFn(index){
		    		var row = shopGrid.getSelectedRows()[0];
		    		if(!row) return $.toast(0,'请先选择店铺！');
		    		$.ajaxs("/admin/v1/user_vip/changeShop",{
		    			uid:uid,
		    			shop_id:row.id
		    		}).then(function(res){
	                    $.toast(1,res.msg);
	                    grid.load();
	                    layer.close(index);
	                });
		    	}
		    	var html =  '<div id="shopGrid" style="padding:3px;"></div>';
		    	$.openModalDialog(html,'请选择需要变更的店铺',successFn,yesFn,['600px', '500px']);
		    }
		    
		    //更新密码
		    ns.updatePwd=function(){
		    	var selectedRow=grid.getSelectedRows()[0];
		    	if(!selectedRow){
		    		$.toast(0,"请先选择用户！");
		    		return;
		    	}
		       function successFn(){
		    		pwdForm = layui.forms.render("updatePwdForm","/system/userMng/config.xml",{
		    		    mode:2,
		    		    oid:selectedRow.id
		    		});
		    	}
		    	function yesFn(index){
		    		var data = pwdForm.getData();
		    		if(!data){
		                return false;
		            }
		            $.ajaxs("/admin/v1/user_vip/updatePwd",{
		            	newPwd:data.user_pwd,
		            	uid: selectedRow.id
		            },function  (res) {
		                grid.load();
		            	layer.close(index);
		            });
		    	}
		    	var html="<div id='updatePwdForm'></div>";
		    	$.openModalDialog(html,"重置密码",successFn,yesFn,["400px","250px"])
		    }
		    
		    
		    return ns;
		}({}));
		
		
		$(function(){ page.init();  });

	</script>
</block>